package com.sunshine.lnuplus.ui.fragment

import android.app.Activity
import android.app.DatePickerDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseFragment
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.ui.activity.MainActivity
import com.sunshine.lnuplus.utils.DateUtils
import com.sunshine.lnuplus.utils.UpdateWidgetUtils
import com.sunshine.lnuplus.utils.LessonUtils
import com.sunshine.lnuplus.utils.TagHelper
import com.sunshine.lnuplus.view.DialogWithoutView
import com.sunshine.lnuplus.view.LoginDialog
import com.sunshine.lnuplus.viewModel.WelcomeFourViewModel
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.fragment_welcome_four.*

class WelcomeFourFragment : BaseFragment(){

    private lateinit var viewModel: WelcomeFourViewModel

    override fun setContent(): Int {
        return R.layout.fragment_welcome_first
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_welcome_four, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(this).get(WelcomeFourViewModel::class.java)
        button_cancle_login.setOnClickListener {
            startActivity(Intent(activity, MainActivity::class.java))
            activity?.finish()
        }
        button_tologin.setOnClickListener {
            val loginDialog = LoginDialog.newInstance(this, TagHelper.WELCOME)
            loginDialog.show(childFragmentManager, "login")
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == TagHelper.WELCOME){
            when(resultCode){
                TagHelper.LOGIN, TagHelper.BANDRR -> {
                    if(LessonUtils.initLessonBean(viewModel.getAllLesson(), context!!, childFragmentManager, viewModel.repository)){
                        DialogWithoutView.newInstance(
                            "提示",
                            "刚刚导入课程，默认的开学时间是每年3月/9月的第一个星期一，是否要更改一下呢",
                            true,
                            DialogInterface.OnClickListener { _, _ ->
                                val sp = context!!.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE)
                                val edit = sp.edit()
                                val month = sp.getInt("start_month", if(DateUtils.month > 8) 8 else 2)
                                val day = sp.getInt("start_day", 1)
                                val datePickerDialog = DatePickerDialog(context!!,
                                    DatePickerDialog.OnDateSetListener { _, _, month, dayOfMonth ->

                                        edit.apply{
                                            putInt("start_month", month)
                                            putInt("start_day", dayOfMonth)
                                            apply()
                                        }
                                        InitData.lessonStyle.startMonth = month
                                        InitData.lessonStyle.startDay = dayOfMonth
                                        Toasty.success(context!!, "设置成功~").show()
                                        startActivity(Intent(activity, MainActivity::class.java))
                                        activity?.finish()
                                    }, DateUtils.year, month, day
                                )
                                datePickerDialog.show()
                            },
                            DialogInterface.OnClickListener { _, _ ->
                                startActivity(Intent(activity, MainActivity::class.java))
                                activity?.finish()
                            }
                        ).show(childFragmentManager, "")
                        UpdateWidgetUtils.updateWidgets(context!!)
                        Toasty.success(context!!, resources.getString(R.string.jiexi_success), Toasty.LENGTH_SHORT).show()
                    }
                }
                Activity.RESULT_CANCELED -> {
                    startActivity(Intent(activity, MainActivity::class.java))
                    activity?.finish()
                }
            }

        }
    }
}